//
//  QLPDFView.m
//  QLDemoProject
//
//  Created by Paramita on 2020/5/9.
//  Copyright © 2020 paramita. All rights reserved.
//

#import "QLPDFView.h"
//#import <MBProgressHUD.h>
//#import <Masonry/Masonry.h>

@interface QLPDFView ()
/// 页码
@property (strong, nonatomic) UILabel *indicatorLabel;

@end

@implementation QLPDFView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configuration];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configuration];
    }
    return self;
}

- (void)configuration {
//    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    
//    self.pdfView.pageBreakMargins = UIEdgeInsetsMake(0, 0, 0, 0);若设置为0  控制台会输出以下2条错误日志
//    [Unknown process name] Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.
//   2021-03-10 14:58:48.560047+0800 PDFKit绘制与签名[20074:5971229] [Unknown process name] If you want to see the backtrace, please set CG_NUMERICS_SHOW_BACKTRACE environmental variable.
    
    self.pdfView = [[LHYPDFView alloc] initWithFrame:self.bounds];
    self.pdfView.pageBreakMargins = UIEdgeInsetsMake(1, 0, 0, 0);
    self.pdfView.delegate = self;
    self.pdfView.autoScales = YES;
    [self addSubview:self.pdfView];
    
    self.indicatorLabel = [[UILabel alloc] init];
    self.indicatorLabel.frame = CGRectMake(self.frame.size.width - 200, 25, 70, 30);
    self.indicatorLabel.layer.cornerRadius = 5;
    self.indicatorLabel.layer.masksToBounds = YES;
    self.indicatorLabel.font = [UIFont systemFontOfSize:15];
    self.indicatorLabel.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3f];
    self.indicatorLabel.textColor = [UIColor whiteColor];
    self.indicatorLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.indicatorLabel];
    
    [self addSubview:self.drawingView];
}


#pragma mark - actions
/// 是否保存
- (BOOL)isNeedSave {
    return [self.drawingView isNeedSave];
}

- (BOOL)saveToFile:(NSString *)path {
    
    NSData *data = self.pdfView.document.dataRepresentation;
    if (data) {
        return [data writeToFile:path atomically:YES];
    }
    return NO;
}

- (void)saveToFile:(NSString *)path block:(void(^)(BOOL suc))block{
    
//    [MBProgressHUD showHUDAddedTo:self animated:YES];
    BOOL suc = NO;
    NSData *data = self.pdfView.document.dataRepresentation;
    
    if (data) {
        suc = [data writeToFile:path atomically:YES];
//        [MBProgressHUD hideHUDForView:self animated:YES];
        self.drawingView.addAnnoCount = 0;
    }
    if (block) {
        block(suc);
    }
}

- (void)nextPage {
    if ([self.pdfView canGoToNextPage]) {
        [self.pdfView goToNextPage:nil];
    }
}

- (void)previousPage {
    if ([self.pdfView canGoToPreviousPage]) {
        [self.pdfView goToPreviousPage:nil];
    }
}
/// 进行绘制状态
- (void)enterDrawingStatus {
    self.drawingView.hidden = NO;
    self.drawingView.action = QLPDFDrawingActionDraw;
    
    self.drawingView.lineWidth = self.lineWidth;
    self.drawingView.lineColor = self.lineColor;
    
    // 重置为最适比例
//    self.pdfView.scaleFactor = self.pdfView.scaleFactorForSizeToFit;
}
/// 退出绘制状态
- (void)exiteDrawingStatus {
    self.drawingView.hidden = YES;
}
/// 进入擦除状态
- (void)enterEraseStatus {
    self.drawingView.hidden = NO;
    self.drawingView.action = QLPDFDrawingActionErase;
}

- (void)gotoPage:(int)index {
    PDFPage *page = [self.pdfView.document pageAtIndex:index];
    if (page) {
        [self.pdfView goToPage:page];
    }
}

- (NSInteger)getPageCount {
    return self.pdfView.document.pageCount;
}
/// 获取当前页数
- (NSInteger)getCurrentPageCount {
    return self.pdfView.currentPage.label.integerValue;
}
/// 获取页高度
- (CGFloat)getPageHeight {
    
    NSArray<UIView *> *arr = self.pdfView.documentView.subviews;
    if (arr.count > 1) {
        PDFPage *page = [self.pdfView.document pageAtIndex:0];
        return [page boundsForBox:kPDFDisplayBoxCropBox].size.height;
    }
    return 792;
}

#pragma mark - set
- (void)setPdfPath:(NSString *)pdfPath {
    _pdfPath = pdfPath;
    if (pdfPath.length) {
        PDFDocument *document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:pdfPath]];
        self.pdfView.document = document;
    }
}

- (void)setLabelStr:(NSString *)labelStr {
    _labelStr = labelStr;
    self.indicatorLabel.text = labelStr;
}

- (void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    self.drawingView.lineColor = lineColor;
}

- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    self.drawingView.lineWidth = lineWidth;
}

- (BOOL)isEdited {
    return self.drawingView.addAnnoCount != 0;
}

//#pragma mark - system
//- (void)layoutSubviews {
//    [super layoutSubviews];
//    [self.pdfView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self);
//    }];
//
//    if (self.drawingView) {
//        [self.drawingView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self);
//        }];
//    }
//}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (QLPDFDrawingView *)drawingView {
    if (!_drawingView) {
        _drawingView = [[QLPDFDrawingView alloc] init];
        _drawingView.frame = self.pdfView.bounds;
        _drawingView.pdfView = self.pdfView;
        _drawingView.hidden = YES;
    }
    return _drawingView;
}






#pragma mark - PDFDocumentDelegate
- (void)documentDidUnlock:(NSNotification *)notification{
    
}

- (void)documentDidBeginDocumentFind:(NSNotification *)notification{
    
}

- (void)documentDidEndDocumentFind:(NSNotification *)notification{
    
}

- (void)documentDidBeginPageFind:(NSNotification *)notification{
    
}

- (void)documentDidEndPageFind:(NSNotification *)notification{
    
}

- (void)documentDidFindMatch:(NSNotification *)notification{
    
}


@end
